package com.yangming.service.impl;

import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.yangming.common.wxpay.WXPayException;
import com.yangming.mapper.EmptyCartServiceMapper;
import com.yangming.mapper.RecordsMapper;
import com.yangming.mapper.SupplyMapper;
import com.yangming.model.EmptyCartService;
import com.yangming.model.EmptyCartServiceRoute;
import com.yangming.model.Records;
import com.yangming.model.Supply;
import com.yangming.service.IPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

/**
 * @author PengShuiMing
 * @create 2018-09-11
 **/
@Service
public class PayServiceImpl implements IPayService {
    @Autowired
    private EmptyCartServiceMapper emptyCartServiceMapper;
    @Autowired
    private SupplyMapper supplyMapper;
    @Autowired
    private RecordsMapper recordsMapper;
    @Autowired
    private WxPayService wxPayService;
    @Transactional
    @Override
    public WxPayMpOrderResult weChatCreateOrder(WxPayUnifiedOrderRequest orderRequest, Integer supplyId, Integer serviceId) throws WXPayException, WxPayException {
        BigDecimal amount = null;

        //获取货源
        //获取空板车服务
        EmptyCartService emptyCartService = emptyCartServiceMapper.selectOne(serviceId);
        Supply supply = supplyMapper.selectByPrimaryKey(supplyId);
        if(emptyCartService.getStatus()!=1){
            throw new WXPayException("该空板车服务已被购买");
        }
        //获取金额
        if (emptyCartService.getDepartureId().equals(supply.getDepartureId())){
            amount = emptyCartService.getPrice();
        }else {
            List<EmptyCartServiceRoute> route = emptyCartService.getRoute();
            if (null==route&&route.size()<=0){
                throw new WXPayException("空板车服务异常");
            }
            for (EmptyCartServiceRoute emptyCartServiceRoute : route) {
                if(emptyCartServiceRoute.getCityId().equals(supply.getDepartureId())){
                    amount = emptyCartServiceRoute.getPrice();
                    break;
                }
            }
        }
        DecimalFormat df = new DecimalFormat("0.00");
        String totalFee = df.format(amount);// 转换为分
        orderRequest.setTotalFee(BaseWxPayRequest.yuanToFen(totalFee));// 订单金额,以分为单位
        //构造订单参数
        WxPayMpOrderResult payResult = wxPayService.createOrder(orderRequest);
        //更新空板车状态
        emptyCartService.setStatus(2);
        emptyCartServiceMapper.updateByPrimaryKeySelective(emptyCartService);
        //新增记录
        Records records = new Records();
        records.setOrderNo(orderRequest.getOutTradeNo());//订单号
        records.setAmount(amount);//金额
        records.setServiceId(emptyCartService.getId());//服务id
        records.setServiceUserId(emptyCartService.getUserId());//服务用户id
        records.setSupplyId(supply.getId());//货源id
        records.setSupplyUserId(supply.getUserId());//货源用户id
        records.setDepartureId(supply.getDepartureId());//出发地id
        records.setDepartureName(supply.getDepartureName());//出发地名称
        records.setIsPay(0);//设置为未支付
        records.setDepartureTime(supply.getDepartureTime());//出发时间
        records.setDestinationId(supply.getDestinationId());//目的地Id
        records.setDestinationName(supply.getDestinationName());//目的地名称
        records.setCreateTime(new Date());//创建时间
        recordsMapper.insertSelective(records);
        return payResult;
    }
}
